package com.cssw.fyzb.wap.util;


import com.ydn.appserver.utils.Base64;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class FileUtil {
    private static final DateFormat FORMAT = new SimpleDateFormat("yyyy/MMdd/");

    /**
     * 将文件转换成byte数组
     *
     * @param file
     * @return
     */
    public static byte[] file2Byte(File file) {
        ByteArrayOutputStream bos = null;
        BufferedInputStream in = null;
        try {
            bos = new ByteArrayOutputStream((int) file.length());
            in = new BufferedInputStream(new FileInputStream(file));
            int buf_size = 1024;
            byte[] buffer = new byte[buf_size];
            int len = 0;
            while (-1 != (len = in.read(buffer, 0, buf_size))) {
                bos.write(buffer, 0, len);
            }
            return bos.toByteArray();
        } catch (IOException e) {
            //    LogUtil.error("file2Byte error", e);
        } finally {
            try {
                if (bos != null) {
                    bos.close();
                }
                if (in != null) {
                    in.close();
                }
            } catch (IOException e) {
                // nothing to do
            }
        }
        return null;
    }

    /**
     * 判断文件大小
     *
     * @param file 文件
     * @param size 限制大小
     * @param unit 限制单位（B,K,M,G）
     * @return
     */
    public static boolean checkFileSize(File file, int size, String unit) {
        long len = file.length();
        double fileSize = 0;
        if ("B".equals(unit.toUpperCase())) {
            fileSize = (double) len;
        } else if ("K".equals(unit.toUpperCase())) {
            fileSize = (double) len / 1024;
        } else if ("M".equals(unit.toUpperCase())) {
            fileSize = (double) len / 1048576;
        } else if ("G".equals(unit.toUpperCase())) {
            fileSize = (double) len / 1073741824;
        }
        if (fileSize > size) {
            return false;
        }
        return true;
    }

    public static String getBase64(File file) {

        return Base64.encodeBytes(file2Byte(file));
    }



    /**
     * @param base
     * @param ，可选值：image/audio/video/document/ebook
     * @param file
     * @return
     */
    private static String mkPath(String base, String file) {
        StringBuilder builder = new StringBuilder(base);
        if (!base.endsWith(File.separator)) {
            builder.append(File.separator);
        }
        return builder
                .append(file)
                .toString();
    }


    /**
     * 写入文件
     *
     * @param resourcePath 资源路径
     * @param content
     * @throws IOException
     */
    private static void writeFile(String resourcePath, byte[] content) throws IOException {
        File file = new File(resourcePath);

        // 创建目录和文件
        if (!file.exists()) {
            File parent = file.getParentFile();
            if (!parent.exists() && !parent.mkdirs()) {
                //  LogUtil.error("Unable to create directory \"" + parent.getPath() + "\"");
                return;
            }

            if (!file.createNewFile()) {
                //  LogUtil.error("Unable to create file \"" + file.getPath() + "\"");
                return;
            }
        }

        // 写入内容
        FileOutputStream fos = new FileOutputStream(file);
        try {
            fos.write(content);
            fos.flush();
        } finally {
            fos.close();
        }

    }


    public static void deleteFile(String path) {
        File file = new File(path);
        if (!file.exists()) {
            return;
        }
        file.delete();
    }

    /**
     * MultipartFile 转 File
     *
     * @param file
     * @throws Exception
     */
    public static File multipartFileToFile(MultipartFile file) throws Exception {

        File toFile = null;
        if (file.equals("") || file.getSize() <= 0) {
            file = null;
        } else {
            InputStream ins = null;
            ins = file.getInputStream();
            toFile = new File(file.getOriginalFilename());
            inputStreamToFile(ins, toFile);
            ins.close();
        }
        return toFile;
    }

    //获取流文件
    private static void inputStreamToFile(InputStream ins, File file) {
        try {
            OutputStream os = new FileOutputStream(file);
            int bytesRead = 0;
            byte[] buffer = new byte[8192];
            while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
                os.write(buffer, 0, bytesRead);
            }
            os.close();
            ins.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static void main(String[] args) throws Exception {

    }


}
